#include "makeWorker.h"

void *workerHandler(void *arg)
{
    threadPool_t *pool = (threadPool_t *)arg;
    char cmd[1024] = {0};
    char cmd_dup[1024] = {0};
    int netfd;
    // char buffer[1024];
    while (1)
    {
        // char buffer[1024];

        pthread_mutex_lock(&pool->mutex);
        while (pool->taskQueue.taskNum == 0 && pool->shutdownFlag == 0)
        {
            pthread_cond_wait(&pool->cond, &pool->mutex);
        }
        if (pool->shutdownFlag == 1 && pool->taskQueue.taskNum == 0)
        {
            pthread_mutex_unlock(&pool->mutex);
            pthread_exit(NULL);
        }
        LOG_INFO("workerHandler:taskQueue.taskNum = %d\n", pool->taskQueue.taskNum);
        // 取出任务
        pthread_mutex_unlock(&pool->mutex);
        taskDequeue(pool, cmd, &netfd);
        strcpy(cmd_dup, cmd);

        // 处理任务
        LOG_INFO("workerHandler:cmd = %s\n", cmd);
        // 解析命令
        char *p = strtok(cmd, " ");
        if (p == NULL)
        {
            LOG_INFO("workerHandler:cmd is NULL\n");
            continue;
        }
        // ls
        if (strcmp(p, "ls") == 0)
        {
            LOG_INFO("workerHandler---deal_ls\n");
            deal_ls(netfd, pool->mysql_pool);
            //获得当前路径发送给客户端
            int pre_path = -1;
            char username[1024] = {0};
            char cur_path[1024] = {0}; // 当前路径
            get_a_session(netfd, pre_path, username);
            LOG_INFO("用户 %s 执行 ls 命令，fd = %d\n", username, netfd);
            get_current_path(pool->mysql_pool, cur_path, pre_path, username); // 获取当前路径
            LOG_INFO("用户 %s 当前路径为 %s\n", username, cur_path);
            // 发送当前路径
            train_t train;
            train.train_len = strlen(cur_path);
            strcpy(train.train_data, cur_path);
            send(netfd, &train, sizeof(train.train_len) + train.train_len, 0);
            LOG_INFO("用户 %s 当前路径发送成功\n", username);
            continue;
        }
        // quit
        else if (strcmp(p, "quit") == 0)
        {
            LOG_INFO("workerHandler:quit\n");
            // char addr[1024];
            deal_quit(pool->epfd, netfd, pool->mysql_pool);

            continue;
        }

        else if (strcmp(p, "put") == 0)
        {
            LOG_INFO("workerHandler:put\n");
            // char addr[1024];
            char *filename = strtok(NULL, " ");
            LOG_INFO("put:filename = %s\n", filename);
            deal_put(netfd, filename, pool->mysql_pool);
            //获得当前路径发送给客户端
            int pre_path = -1;
            char username[1024] = {0};
            char cur_path[1024] = {0}; // 当前路径
            get_a_session(netfd, pre_path, username);
            LOG_INFO("用户 %s 执行 ls 命令，fd = %d\n", username, netfd);
            get_current_path(pool->mysql_pool, cur_path, pre_path, username); // 获取当前路径
            LOG_INFO("用户 %s 当前路径为 %s\n", username, cur_path);
            // 发送当前路径
            train_t train;
            train.train_len = strlen(cur_path);
            strcpy(train.train_data, cur_path);
            send(netfd, &train, sizeof(train.train_len) + train.train_len, 0);
            LOG_INFO("用户 %s 当前路径发送成功\n", username);

            continue;
        }
        // cd
        //  else if (strcmp(p, "cd") == 0){
        //      LOG_INFO("workerHandler:cd\n");
        //      char addr[1024];
        //      deal_cd(netfd,addr,pool->mysql_pool);
        //  }
        //  //put
        //  else if (strcmp(p, "put") == 0){
        //      LOG_INFO("workerHandler:put\n");
        //      char addr[1024];
        //      deal_put(netfd,addr,pool->mysql_pool);
        //  }
        {
            /* code */
        }

        // doTask(task);
        //  free(task);
    }
    return NULL;
}

// 创建工人线程
int workerInit(threadPool_t *pool, int workerNum)
{
    // 创建工人线程
    for (int i = 0; i < workerNum; i++)
    {
        int ret = pthread_create(&pool->worker.tid[workerNum], NULL, workerHandler, pool);
        ERROR_CHECK(ret, -1, "pthread_create");
        LOG_INFO("create %d worker thread success", i);
    }
    return 0;
}
